function search(arr,target) {
  let left = 0;
  let right = arr.length - 1;
  while(left <= right) {
    let mid = Math.round((left+right) / 2);
    if(arr[mid] === target) {
      return mid;
    } else if(arr[mid] < target) {
      left = mid + 1;
    } else {
      right = mid - 1
    }
  }
  return left;
}

console.log(search([1,3,5,6], 5));